package com.sensologyhome_all.net;

import android.content.Context;

import com.sensologyhome_all.eventbus.ErrorEvent;
import com.sensologyhome_all.util.ConstUtil;
import com.sensologyhome_all.util.JsonUtil;
import com.sensologyhome_all.util.NetUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import okhttp3.MediaType;

/**
 * Created by xupeixian on 2017/2/9.
 */

public class MyHttpUtil {
    public static void getCode(Context context, Map<String, String> params, MyCallback callback) {
        String url = ConstUtil.BASE_HOST_URL + "info/user/getValidateCode";
        get(context,url, params, callback);
    }

    public static void postString(Context context, String url, Map<String, String> params, MyStringCallback callback)
    {
        OkHttpUtils
                .postString()
                .url(url)
                .tag(context)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(JsonUtil.parseMapToJson(params))
                .build()
                .execute(callback);
    }

    private static void postJsonBean(Context context, String url, Object obj, MyCallback callback) {
        OkHttpUtils
                .postString()
                .url(url)//
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(JsonUtil.parseBeanToJson(obj))
//                .addHeader("token", SharedPrefUtil.getToken())
//                .addHeader("userId", SharedPrefUtil.getUserId())
                .tag(context)
                .build()//
                .execute( callback);
    }


    public static void post(Context context, String url, Map<String, ? extends Object> params, final MyCallback callback) {
        if(!NetUtil.checkNetworkIfAvailable(context)){
            EventBus.getDefault().post( new ErrorEvent(ConstUtil.HTTP_NET_ERROR,"请打开网络", context, context));
            return;
        }
        OkHttpUtils
                .postString()
                .url(url)//
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(JsonUtil.parseMapToJson(params))
//                .addHeader("token", SharedPrefUtil.getToken())
//                .addHeader("userId", SharedPrefUtil.getUserId())
                .tag(context)
                .build()//
                .execute( callback);
    }


    public static void get(Context context, String url, Map<String, String> params, final MyCallback callback) {
        if(!NetUtil.checkNetworkIfAvailable(context)){
            EventBus.getDefault().post( new ErrorEvent(ConstUtil.HTTP_NET_ERROR,"请打开网络", context, context));
            return;
        }
        OkHttpUtils
                .get()//
                .url(url)//
                .params(params)
                .tag(context)
                .build()//
                .execute(callback);
    }



   /* public static void postFile(Context context, String url, File file, MyCallback callback) {
        if(!NetUtil.checkNetworkIfAvailable(context)){
            EventBus.getDefault().post( new ErrorEvent(ConstUtil.HTTP_NET_ERROR,"请打开网络", context, context));
            return;
        }
        if (!file.exists())
        {
            PrintUtil.toast(context, "文件不存在，请修改文件路径");
            return;
        }

        Map<String, String> params = new HashMap<>();
        params.put("token", SharedPrefUtil.getToken());
        params.put("userId", SharedPrefUtil.getUserId());

        Map<String, String> headers = new HashMap<>();
        headers.put("token", SharedPrefUtil.getToken());
        headers.put("userId", SharedPrefUtil.getUserId());
        OkHttpUtils.post()//
                .tag(context)
                .addFile("image", file.getName(), file)//
                .url(url)//
                .params(params)//
                .headers(headers)//
                .build()//
                .execute(callback);
    }

    public static void postFiles(Context context, String url, ArrayList<File> files, Map<String, String> params, MyCallback callback) {
        if(!NetUtil.checkNetworkIfAvailable(context)){
            EventBus.getDefault().post( new ErrorEvent(ConstUtil.HTTP_NET_ERROR,"请打开网络", context, context));
            return;
        }
        Map<String,File> fileMap=new HashMap<String,File>();
        if (files.size()>0){
            for (File file : files){
                fileMap.put(file.getName(),file);
            }
        }
        Map<String, String> headers = new HashMap<>();
        headers.put("token", SharedPrefUtil.getToken());
        headers.put("userId", SharedPrefUtil.getUserId());
        OkHttpUtils.post()//
                .tag(context)
                .files("images",fileMap)
                .url(url)//
                .params(params)//
                .headers(headers)//
                .build()//
                .execute(callback);
    }


    public static void postMultiFile(Context context, String url, Map<String,File> files, Map<String, String> params, MyCallback callback) {
        if(!NetUtil.checkNetworkIfAvailable(context)){
            EventBus.getDefault().post( new ErrorEvent(ConstUtil.HTTP_NET_ERROR,"请打开网络", context, context));
            return;
        }
        Map<String, String> headers = new HashMap<>();
        headers.put("token", SharedPrefUtil.getToken());
        headers.put("userId", SharedPrefUtil.getUserId());
        OkHttpUtils.post()//
                .tag(context)
//                .files("images",files)
                .addFile("businessLicense","businessLicense",files.get("businessLicense"))
                .addFile(files.containsKey("transportPermit")?"transportPermit":"nvocc",files.containsKey("transportPermit")?"transportPermit":"nvocc",files.get(files.containsKey("transportPermit")?"transportPermit":"nvocc"))
                .url(url)//
                .params(params)//
                .headers(headers)//
                .build()//
                .execute(callback);
    }
*/




    public static void cancelCall(Context context){
        OkHttpUtils.getInstance().cancelTag(context);
    }
}